package org.vectrics.recruiter.job.event;

import java.util.Locale;

import org.vectrics.common.html.BorderTable;
import org.vectrics.common.html.Cell;
import org.vectrics.common.util.DateFormats;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.email.RecruitEmailHtmlDefault;
import org.vectrics.recruiter.job.Job;
import org.vectrics.recruiter.job.JobCandidate;
import org.vectrics.recruiter.user.CandidateInfo;
import org.vectrics.recruiter.user.Interview;

public class InterviewHtmlEmail extends RecruitEmailHtmlDefault {
	private Interview interview = null;
	private int tableWidth = 600;
	
	public InterviewHtmlEmail(Locale locale, Interview interview) {
		super(interview.getDomain(), locale);
		this.interview = interview;
	}

	protected void renderContent(Cell cell) {
		cell.addElement("<br><br><font style=\"font:normal 0.8em Verdana\">"
				+ this.getPhrase("phrase.email.interview.changed") + "</font><br>");
		this.addInterviewInfoTable(this.interview, cell);
		
		Job job = interview.getJob();
		CandidateInfo candidateInfo = RecruitServiceLocator.getRecruitUserService().findCandidateInfo(interview.getPerson());
		if (job != null) {
			if (candidateInfo != null) {
				job = RecruitServiceLocator.getJobProvider().findJob(job.getId());
				JobCandidate jobCandidate = job.findJobCandidate(candidateInfo);
				if (jobCandidate != null) {
					cell.addElement("<br><br><font style=\"font:normal 0.8em Verdana\">"
							+ this.getPhrase("phrase.job.candidate.information") + "</font><br>");
					cell.addElement(new JobCandidateInfoEmailTable(this.getLocale(), jobCandidate, false));
				}
			}
			
			cell.addElement("<br><br><font style=\"font:normal 0.8em Verdana\">"
					+ this.getPhrase("phrase.job.information") + "</font><br>");
			cell.addElement(new JobInfoEmailTable(this.getLocale(), job));
		}
	}

	
	public void addInterviewInfoTable(Interview interview, Cell mainCell) {
		interview = RecruitServiceLocator.getRecruitUserService().findInterviewById(interview.getId());
		DateFormats dateFormats = CoreServiceLocator.getDateFormat(); 
		
		BorderTable table = new BorderTable();
		table.setWidth(tableWidth);
		mainCell.addElement(table);

		Job job = RecruitServiceLocator.getJobProvider().findJob(interview.getJob().getId());
		CandidateInfo candidateInfo = null;
		if (job != null) {
			candidateInfo = RecruitServiceLocator.getRecruitUserService().findCandidateInfo(interview.getPerson());
			if (candidateInfo != null) {
				job = RecruitServiceLocator.getJobProvider().findJob(interview.getJob().getId());
			}
		}
		
		appendPropertyRow( 
				this.getPhrase("prompt.job.company"), 
				job.getCompany().getName(), 
				table);
		
		appendPropertyRow( 
				this.getPhrase("prompt.job.candidate"), 
				interview.getPerson().getName(), 
				table);

		appendPropertyRow( 
				this.getPhrase("prompt.interview.description"), 
				interview.getDescription(), 
				table);

		appendPropertyRow( 
				this.getPhrase("prompt.interview.followup"), 
				interview.getFollowUp(), 
				table);

		if (interview.getDateStart() != null) {
			appendPropertyRow( 
					this.getPhrase("prompt.interview.date.start"), 
					dateFormats.getDateFormatRoundToMinute(this.getLocale())
							.format(interview.getDateStart()), 
					table);
		} else {
			appendPropertyRow( 
					this.getPhrase("prompt.interview.date.start"), 
					this.getPhrase("phrase.not.given"), 
					table);
		}
		
		if (interview.getDateEnd() != null) {
			appendPropertyRow( 
					this.getPhrase("prompt.interview.date.end"), 
					dateFormats.getDateFormatRoundToMinute(this.getLocale())
							.format(interview.getDateEnd()), 
					table);
		} else {
			appendPropertyRow( 
					this.getPhrase("prompt.interview.date.end"), 
					this.getPhrase("phrase.not.given"), 
					table);
		}

		appendPropertyRow( 
				this.getPhrase("prompt.interview.notes"), 
				interview.getNotes(), 
				table);

		appendPropertyRow( 
				this.getPhrase("prompt.interview.last.changed.by"), 
				interview.getLastModifiedBy().getName(), 
				table);
/*		
		if (job != null) {
			appendPropertyRow( 
					this.getPhrase("prompt.job.reference.id"), 
					job.getReferenceId(), 
					table);
		
			appendPropertyRow( 
					this.getPhrase("prompt.job.title"), 
					job.getTitle(), 
					table);
			
			appendPropertyRow( 
					this.getPhrase("prompt.job.status"), 
					this.getPhrase(job.getCurrentStatus().getStatusType().getDescriptionCode())
					+ "<br><i>" + this.getPhrase(job.getCurrentStatus().getStatusReasonType().getName())
					+ "</i>", 
					table);
			
			if (job.getPrimaryRecruiter() != null) {
				appendPropertyRow( 
						this.getPhrase("prompt.job.primary.recruiter"), 
						job.getPrimaryRecruiter().getName(), 
						table);
			}
			appendPropertyRow( 
					this.getPhrase("prompt.job.hiring.manager"), 
					job.getHiringManager().getName(), 
					table);
		}
		
		if (candidateInfo != null) {
			appendPropertyRow( 
					this.getPhrase("prompt.candidate.currently.working"), 
					"" + candidateInfo.getCurrentlyWorking(), 
					table);
			
		}
		
		if (jobCandidate != null) {
			appendPropertyRow( 
					this.getPhrase("prompt.job.candidate.status"), 
					jobCandidate.getCurrentStatus().getStatusType().getDescription(), 
					table);
			
			appendPropertyRow( 
					this.getPhrase("prompt.job.candidate.date.created"), 
					CoreServiceLocator.getDateFormat().getDateFormatRoundToDay(this.getLocale()).format(jobCandidate.getDateAdded()), 
					table);
		}
		*/
	}
	
}
